// pages/note/index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAuthorize: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获得openid
    var that = this;
    that.getopenid();
    //据openid 从后台取出 昵称 图片地址 今天拜佛数 念佛数
    //   that.getdata(app.get_openid);
    //如果 有 后台取出的昵称 则不显示弹窗 否则显示弹窗


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /*
    login: function (info) {
  console.log(info)
  console.log(info.code)
    }
  /*
     getUserInfo: function (e) {
      console.log(e)
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    }
   
  */
  getopenid: function () {
    var that = this;
    //获得openid开始
    wx.login({
      success(res) {
        if (res.code) {
          //  console.log(res.code)
          //发起网络请求
          wx.request({
            url: 'https://www.hyhthjk.com.cn/nfbf.php/index/Indexxcx/index4',
            data: {
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: function (res) {
              //  console.log(res.data)
              app.openid = res.data[0].openid;
              app.headimgurl = res.data[0].headimgurl;
              app.nickname = res.data[0].nickname;
              app.nfZongNub = res.data[0].nfZongNub;
              app.date_yx = res.data[0].date_yx;
              app.bfZongNub = res.data[0].bfZongNub;
              app.date_bf = res.data[0].date_bf;
              app.bfNub = res.data[0].bfNub;
              app.nfNub = res.data[0].nfNub;
              app.my_qun = res.data[0].qun;
              app.yesterday_bf = res.data[0].yesterday_bf;
              app.yesterday_nf = res.data[0].yesterday_nf;
             

              that.setData({
                isAuthorize: res.data[0].isAuthorize,
                headimgurl: res.data[0].headimgurl,
                nickname: res.data[0].nickname,
                nfNub: res.data[0].nfNub,
                bfNub: res.data[0].bfNub,
              })
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }

    })
    //获得openid 结束
  },
  refuse: function refuse() {
    var that = this;
    that.setData({
      isAuthorize: false
    });

  },

  login: function (e) {
    var that = this
    app.globalData.userInfo = e.detail.userInfo
    //把公开信息传给服务器
    console.log(app.openid)
    console.log(app.globalData.userInfo.nickName);
    console.log(app.globalData.userInfo);
    wx.request({
      url: 'https://www.hyhthjk.com.cn/nfbf.php/index/Indexxcx/index5',
      data: {
        openid: app.openid,
        nickname: app.globalData.userInfo.nickName,
        headimgurl: app.globalData.userInfo.avatarUrl,
        address: app.globalData.userInfo.country + app.globalData.userInfo.province + app.globalData.userInfo.city
      },
      header: {
        'content-type': 'application/json' // 默认值
      },

      success: function (res) {
        console.log(res)
        that.setData({
          userInfo: e.detail.userInfo,
          isAuthorize: false,
          /* hasUserInfo: true*/
          headimgurl: app.globalData.userInfo.avatarUrl,
          nickname: app.globalData.userInfo.nickName,
        })
        console.log(isAuthorize)
      }
    })
  },
  search: function (e) {
    console.log('form发生了submit事件，携带数据note为：', e.detail.value)
    var input = e.detail.value.input;
    // console.log('念佛数'+e.detail.value.nfnub)
    // console.log('openid' +app.openid )
    wx.request({
      url: 'https://www.hyhthjk.com.cn/nfbf.php/index/Indexxcx/index6',
      data: {
        openid: app.openid, //把input的值给后台服务器
        nfNub: e.detail.value.nfnub,
        bfNub: e.detail.value.bfnub
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.showModal({
          title: '报数成功',
        })
      }
    })
  },
  //监听 onPostTap2 开始
  onPostTap2: function (event) {
    wx.navigateTo({
      url: "../note/shezhi/index"
    })

  },
  //监听 onPostTap 开始
  onPostTap: function (event) {
    wx.navigateTo({
      url: "../note/report/index"
    })

  }
})